package com.link.base.base.project.service;

import com.link.base.base.project.dao.mybatis.mapper.PartAnnualSaleMapper;
import com.link.base.base.project.model.PartAnnualSale;
import com.link.base.core.basic.util.BigDecimalUtils;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 零件类型年度销售额表
 *
 * @author 王昊
 * @date 2021-05-07 14:34:55
 */
@Service
public class PartAnnualSaleServiceImpl extends BasicServiceImpl<PartAnnualSale> implements PartAnnualSaleService {

    @Resource
    private PartAnnualSaleMapper    partAnnualSaleMapper;

    @Override
    public BasicMapper<PartAnnualSale> getBasicMapper() {
        return partAnnualSaleMapper;
    }

    /**
     * 前20车企各零件销售额分析（饼图）
     * 根据年份和客户查询零件销售额
     * @param entity
     * @return
     */
    @Override
    public List<PartAnnualSale> queryAllByYearAcct(PartAnnualSale entity) throws Exception {
        return partAnnualSaleMapper.queryAllByYearAcct(entity);
    }

    /**
     * 前20畅销车型各零件销售额分析（饼图）
     * 根据年份和畅销车型名称查询销售额
     */
    @Override
    public List<PartAnnualSale> queryAllByYearCarModel(PartAnnualSale entity) throws Exception {
        return partAnnualSaleMapper.queryAllByYearCarModel(entity);
    }

    /**
     * 各零件类型年度销售额分析（嵌套环形图）
     * 根据年份查询各零件年度销售额
     */
    @Override
    public List<PartAnnualSale> queryAllByYear(PartAnnualSale entity) throws Exception {
        List<PartAnnualSale> partAnnualSales = partAnnualSaleMapper.queryAllByYear(entity);
        if (!partAnnualSales.isEmpty() && partAnnualSales != null) {
            //主导产品
            BigDecimal sumLeadingProduct = BigDecimal.ZERO;
            //非主导产品
            BigDecimal sumNoLeadingProduct = BigDecimal.ZERO;

            //分别遍历查到的零件销售额记录，将所有记录量产销售额相加，赋值给每一条记录的attr3字段
            for (PartAnnualSale partAnnualSale : partAnnualSales) {
                BigDecimal massProdSales = BigDecimalUtils.changeNullToZero(partAnnualSale.getMassProdSales());
                String partKind = partAnnualSale.getPartKind() == null ? "badPartKind" : partAnnualSale.getPartKind();
                if ("LeadingProduct".equals(partKind)) {
                    sumLeadingProduct = sumLeadingProduct.add(massProdSales);
                } else if ("NoLeadingProduct".equals(partKind)) {
                    sumNoLeadingProduct = sumNoLeadingProduct.add(massProdSales);
                } else {
                    //该年度未获取到部分零件类别，请检查零件类别是否维护正确！
                    throw new ServiceException("IYUPROJECT-013");
                }
            }
            for (PartAnnualSale partAnnualSale : partAnnualSales) {
                //给每一条记录的attr3 赋值主导产品的销售额总和，attr4赋值非主导产品销售额总和
                partAnnualSale.setAttr3(String.valueOf(sumLeadingProduct));
                partAnnualSale.setAttr4(String.valueOf(sumNoLeadingProduct));
            }
        }
        return partAnnualSales;
    }
}